﻿/*
 * 开发人：Wubinner
 * 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Partner.MVC.Models;

namespace Partner.MVC.Controllers
{
    public class AdminCenterController : Controller
    {
        GuyEntities db = new GuyEntities();

        #region  我的审核
        /// <summary>
        ///  我的审核 -- 房源列表
        /// </summary>
        /// <returns></returns>
        public ActionResult HouseList()
        {
            var houselist = db.House_Source.Where(a => a.House_State != 4).ToList();
            ViewBag.state0 = houselist.Where(a => a.House_State == 0).ToList();
            ViewBag.state1 = houselist.Where(a => a.House_State == 1).ToList();
            ViewBag.state2 = houselist.Where(a => a.House_State == 2).ToList();
            return View();  //  0未审核  1通过    2 驳回     3用户删除   4 管理员删除
        }

        /// <summary>
        ///  我的审核 -- 服务列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ServerList()
        {
            var serverList = db.House_Server.Where(a => a.IsUse != 4).ToList();
            ViewBag.isUse0 = serverList.Where(a => a.IsUse == 0).ToList();
            ViewBag.isUse1 = serverList.Where(a => a.IsUse == 1).ToList();
            ViewBag.isUse2 = serverList.Where(a => a.IsUse == 2).ToList();
            return View();  //  0未审核  1通过    2 驳回     3 商家删除   4 管理员删除
        }

        /// <summary>
        /// 通过服务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int ServerPass(int id)
        {
            var sid = db.House_Server.Where(a => a.S_Id == id).FirstOrDefault();
            sid.IsUse = 1;
            db.SaveChanges();

            House_Message m = new House_Message();
            m.IsDel = 0;
            m.Use_Id = sid.S_CreatePerson;
            m.M_Title = "您发布的服务" + sid.S_Name + "通过审核啦";
            m.M_Content = "您在" + sid.S_CreaeteTime + "发布的服务" + sid.S_Name + "通过审核啦，请关注客户订单哟！";
            m.M_Time = DateTime.Now;
            db.House_Message.Add(m);
            return db.SaveChanges();
        }

        /// <summary>
        /// 驳回服务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int ServerBack(int id)
        {
            var sid = db.House_Server.Where(a => a.S_Id == id).FirstOrDefault();
            sid.IsUse = 2;
            db.SaveChanges();

            House_Message m = new House_Message();
            m.IsDel = 0;
            m.Use_Id = sid.S_CreatePerson;
            m.M_Title = "您发布的服务" + sid.S_Name + "未通过";
            m.M_Content = "您在" + sid.S_CreaeteTime + "发布的服务" + sid.S_Name + "审核未通过，请重新填写信息进行审核！";
            m.M_Time = DateTime.Now;
            db.House_Message.Add(m);
            return db.SaveChanges();
        }

        /// <summary>
        /// 通过房源
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int HousePass(int id)
        {
            var sid = db.House_Source.Where(a => a.House_Id == id).FirstOrDefault();
            sid.House_State = 1;
            db.SaveChanges();

            House_Message m = new House_Message();
            m.IsDel = 0;
            m.Use_Id = sid.House_CreatePeople;
            m.M_Title = "您发布的房源" + sid.House_Name + "通过啦";
            m.M_Content = "您在" + sid.House_CreateTime + "发布的房源" + sid.House_Name + "通过审核啦，请关注租户预约哟！";
            m.M_Time = DateTime.Now;
            db.House_Message.Add(m);
            return db.SaveChanges();
        }

        /// <summary>
        /// 驳回房源
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int HouseBack(int id)
        {
            var sid = db.House_Source.Where(a => a.House_Id == id).FirstOrDefault();
            sid.House_State = 2;
            db.SaveChanges();

            House_Message m = new House_Message();
            m.IsDel = 0;
            m.Use_Id = sid.House_CreatePeople;
            m.M_Title = "您发布的房源" + sid.House_Name + "未通过";
            m.M_Content = "您在" + sid.House_CreateTime + "发布的房源" + sid.House_Name + "审核未通过，请重新填写信息进行审核！";
            m.M_Time = DateTime.Now;
            db.House_Message.Add(m);
            return db.SaveChanges();
        }
        #endregion

        #region 委托记录
        /// <summary>
        ///  委托列表
        /// </summary>
        /// <returns></returns>
        public ActionResult EntrustList()
        {
            var user = Session["LoginUser"] as User_Info;
            ViewBag.beSend = db.House_Entrust.Where(a => a.E_State == 0 && a.E_State != 3).ToList();  // 0未处理 1 已处理  2用户删除  3 管理员删除
            ViewBag.Send = db.House_Entrust.Where(a => a.E_State == 1 && a.E_State != 3).ToList();
            return View();  // 查询委托表
        }

        /// <summary>
        ///  委托详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EntrustXX(int id)
        {
            var house = db.House_Source.ToList();
            var Entrust = (from e in db.House_Entrust
                           join u in db.User_Info
                           on e.E_CreatePeople equals u.Use_Id
                           where e.E_Id == id
                           select new
                           {
                               e.E_Address,
                               e.E_Area,
                               e.E_CreateTime,
                               e.E_Decorate,
                               e.E_HopeSell,
                               e.E_Id,
                               e.E_Price,
                               e.E_Remark,
                               e.E_State,
                               e.E_Telphone,
                               e.E_Type,
                               u.Use_Name,
                               e.E_CreatePeople,
                           });
            var EntrustModel = (from i in Entrust.ToList()
                                select Tuple.Create(
                                i.E_Type,
                                i.E_Decorate,
                                i.E_Area,
                                i.E_Address,
                                i.E_Price,
                                i.E_HopeSell,
                                Tuple.Create(
                                i.E_Telphone,
                                i.E_Remark,
                                i.Use_Name,
                                i.E_CreateTime,
                                i.E_CreatePeople,
                                i.E_Id
                                )
                                )).FirstOrDefault();
            house = house.Where(a => a.House_Type == EntrustModel.Item1).ToList();
            ViewBag.houseList = house;
            return View(EntrustModel);
        }

        /// <summary>
        ///  房源推送
        /// </summary>
        /// <param name="House_Id"></param>
        /// <param name="useid"></param>
        /// <returns></returns>
        public int SendHouse(string House_Id, int useid, int txtId)
        {
            var house = "";
            if (House_Id.Length > 1)
            {
                string[] id = House_Id.Split(',');
            }
            else
            {
                int Houseid = Convert.ToInt32(House_Id);
                house = db.House_Source.Where(a => a.House_Id == Houseid).FirstOrDefault().House_Name;
            }
            var entrust = db.House_Entrust.Where(a => a.E_Id == txtId).FirstOrDefault();
            entrust.E_State = 1;  //  委托表状态，已推送
            db.SaveChanges();

            House_Message m = new House_Message();
            m.Use_Id = useid;
            m.M_Title = "房源推送";
            m.M_Content = "" + House_Id + "";
            m.M_Time = DateTime.Now;
            m.IsDel = 0;   //0未读，1已读，2删除
            db.House_Message.Add(m);
            return db.SaveChanges();
        }

        #endregion

    }
}
